<?php

class Gmapsprofile_Widget_PlacesFriendsController extends Engine_Content_Widget_Abstract
{
  protected $_requireProfileType = true;	

  public function indexAction()
  {
    $pageSubject = Engine_Api::_()->core()->hasSubject() ? Engine_Api::_()->core()->getSubject() : null;
	if( !$pageSubject ) return $this->setNoRender();	
	$viewer = Engine_Api::_()->user()->getViewer();
	$user = $viewer;
	if ($pageSubject) {
	  $this->view->user = $user = $pageSubject;
	  $translate = Zend_Registry::get('Zend_Translate');
	  $userLanguage = $user->language;
      if( !Zend_Locale::isLocale($userLanguage) ||
          $userLanguage == 'auto' ||
          !in_array($userLanguage, $translate->getList()) ) {
        $userLanguage = $translate->getLocale();
      }	  
	  $this->view->userLanguage = isset($userLanguage) ? $userLanguage : 'en';
	} 
	
	if( !Engine_Api::_()->authorization()->isAllowed('gmapsprofile', null, 'viewfriend') ) return $this->setNoRender();	 
	
	$select = $user->membership()->getMembersOfSelect();
	$this->view->friends = $friends = $paginator = Zend_Paginator::factory($select);
	$paginator->setItemCountPerPage(1000000);
    $ids = array();
    foreach( $friends as $friend ) {
      $ids[] = $friend->resource_id;
    }	
	$this->view->friendIds = $ids;
    // Get the items
    $friendUsers = array();
    foreach( Engine_Api::_()->getItemTable('user')->find($ids) as $friendUser ) {
      $friendUsers[$friendUser->getIdentity()] = $friendUser;
    }	
	$this->view->friendUsers = $friendUsers;
	
    $settings = Engine_Api::_()->getApi('settings', 'core');
	
	$this->view->scale = isset($settings->gmapsprofile_scale_friends) ? $settings->gmapsprofile_scale_friends : 2;
	$this->view->map_type = isset($settings->gmapsprofile_tm_friends) ? $settings->gmapsprofile_tm_friends : 'ROADMAP';		
  }
  
}  